package com.hp.team.bookstore.dbutil.init;

import com.hp.team.bookstore.dbutil.dao.DAOFactory;
import com.hp.team.bookstore.dbutil.dao.HibernateUtil;
import com.hp.team.bookstore.dbutil.dao.profile.RoleHibernateDAO;
import com.hp.team.bookstore.model.ICommonConstants;
import com.hp.team.bookstore.model.profile.Role;

public class RoleInitializer {

	private static RoleHibernateDAO dao;

	static {
		DAOFactory daoFactory = new DAOFactory();
		dao = daoFactory.getRoleDAO();
	}

	public static void init() {
		Role roleManager = createRole(ICommonConstants.ROLE_MANAGER);
		Role rolePurchaseSpecialist = createRole(ICommonConstants.ROLE_PURCHASE_SPECIALIST);
		Role roleShippingSpecialist = createRole(ICommonConstants.ROLE_SHIPPING_SPECIALIST);
		Role roleInventoryManager = createRole(ICommonConstants.ROLE_INVENTORY_MANAGER);
		Role roleRetailer = createRole(ICommonConstants.ROLE_RETAILER);
		Role roleContentEditor = createRole(ICommonConstants.ROLE_CONTENT_EDITOR);

		dao.setSession(HibernateUtil.getSessionFactory().getCurrentSession());

		HibernateUtil.beginWriteTransaction();
		dao.makePersistent(roleInventoryManager);
		dao.makePersistent(roleManager);
		dao.makePersistent(rolePurchaseSpecialist);
		dao.makePersistent(roleShippingSpecialist);
		dao.makePersistent(roleRetailer);
		dao.makePersistent(roleContentEditor);
		HibernateUtil.commitTransaction();
	}

	private static Role createRole(String name) {
		Role role = new Role();
		role.setName(name);
		return role;
	}

	public static void main(String[] args) {
		RoleInitializer.init();
	}

}
